package controllers

import com.jfinal.aop.Before
import com.jfinal.core.Controller
import controllers.interceptors.UserTokenCheckValidator
import services.RecordService

@Before(UserTokenCheckValidator::class)
class RecordController: Controller() {

    companion object{
        const val PARA_RECORD_ID = "record_id"
        const val PARA_COLLECT_FLAG = "flag"
    }

    private val service: RecordService by lazy {
        RecordService(this)
    }

    fun publishList(){
        val userId = UserTokenCheckValidator.getUserId(this)
        service.getPublicRecordByUserId(userId)
    }

    fun collectList(){
        val userId = UserTokenCheckValidator.getUserId(this)
        service.getCollectionByUserId(userId)
    }

    fun checkCollect(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val recordId = getInt(PARA_RECORD_ID)
        service.checkCollect(userId, recordId)
    }

    fun collect(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val recordId = getInt(PARA_RECORD_ID)
        val flag = getBoolean(PARA_COLLECT_FLAG)
        service.collectOrCancel(userId, recordId, flag)
    }

    fun boughtList(){
        val userId = UserTokenCheckValidator.getUserId(this)
        service.getBoughtRecordByUserId(userId)
    }

    fun soldList(){
        val userId = UserTokenCheckValidator.getUserId(this)
        service.getSoldRecordByUserId(userId)
    }
}